package com.andlabs.gd.test;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

import com.andlabs.gd.R;
import com.andlabs.gd.base.Point;

public class Knight implements Character{
	//the bitmaps displayed when standing, one for each of the eight directions
	private Bitmap mStandingEast;
	private Bitmap mStandingSouthEast;
	private Bitmap mStandingSouth;
	private Bitmap mStandingSouthWest;
	private Bitmap mStandingWest;
	private Bitmap mStandingNorthWest;
	private Bitmap mStandingNorth;
	private Bitmap mStandingNorthEast;

	//the bitmaps displayed when walking, should all have the same size, one for each of the eight directions
	private List<Bitmap> mWalkingSequenceEast = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceSouthEast = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceSouth = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceSouthWest = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceWest = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceNorthWest = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceNorth = new ArrayList<Bitmap>();
	private List<Bitmap> mWalkingSequenceNorthEast = new ArrayList<Bitmap>();

	//the next bitmap to be displayed
	private Bitmap mNextBitmap;

	//the last displayed image in the sequence
	private int mNextInSequence = 0;
	//the time after a image has to be changed
	private float mChangeSequenceTime;
	//the time elapsed since the last change in the sequence
	private float mLastChangeElapsedTime = 0;
	
	//speed in pixel per second
	private static final float VELOCITY = 100;
	//length of one walking sequence
	private static final float WALKING_SEQUENCE_LENGTH = 0.8f;
	//the four directions
	private static final int NORTH = -1;
	private static final int EAST = 1;
	private static final int SOUTH = 1;
	private static final int WEST = -1;
	
	//the current direction
	private int mDirectionY;
	private int mDirectionX;
	
	//the position
	Point mPosition = new Point();
	
	//the destination to go
	public int mDestinationX = (int) mPosition.x;
	public int mDestinationY = (int) mPosition.y;
	
	private int mSpriteWidth;
	private int mSpriteHeight;
	
	public Knight(Context context){
		mStandingEast = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_e);
		Bitmap step11e = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_e);
		Bitmap step12e = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_e);
		Bitmap step21e = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_e);
		Bitmap step22e = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_e);
	
		mWalkingSequenceEast.add(mStandingEast);
		mWalkingSequenceEast.add(step11e);
		mWalkingSequenceEast.add(step12e);
		mWalkingSequenceEast.add(step11e);
		mWalkingSequenceEast.add(mStandingEast);
		mWalkingSequenceEast.add(step21e);
		mWalkingSequenceEast.add(step22e);
		mWalkingSequenceEast.add(step21e);	
		
		mStandingSouthEast = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_se);
		Bitmap step11se = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_se);
		Bitmap step12se = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_se);
		Bitmap step21se = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_se);
		Bitmap step22se = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_se);
	
		mWalkingSequenceSouthEast.add(mStandingSouthEast);
		mWalkingSequenceSouthEast.add(step11se);
		mWalkingSequenceSouthEast.add(step12se);
		mWalkingSequenceSouthEast.add(step11se);
		mWalkingSequenceSouthEast.add(mStandingSouthEast);
		mWalkingSequenceSouthEast.add(step21se);
		mWalkingSequenceSouthEast.add(step22se);
		mWalkingSequenceSouthEast.add(step21se);	
		
		mStandingSouth = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_s);
		Bitmap step11s = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_s);
		Bitmap step12s = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_s);
		Bitmap step21s = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_s);
		Bitmap step22s = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_s);
	
		mWalkingSequenceSouth.add(mStandingSouth);
		mWalkingSequenceSouth.add(step11s);
		mWalkingSequenceSouth.add(step12s);
		mWalkingSequenceSouth.add(step11s);
		mWalkingSequenceSouth.add(mStandingSouth);
		mWalkingSequenceSouth.add(step21s);
		mWalkingSequenceSouth.add(step22s);
		mWalkingSequenceSouth.add(step21s);	
		
		mStandingSouthWest = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_sw);
		Bitmap step11sw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_sw);
		Bitmap step12sw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_sw);
		Bitmap step21sw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_sw);
		Bitmap step22sw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_sw);
	
		mWalkingSequenceSouthWest.add(mStandingSouthWest);
		mWalkingSequenceSouthWest.add(step11sw);
		mWalkingSequenceSouthWest.add(step12sw);
		mWalkingSequenceSouthWest.add(step11sw);
		mWalkingSequenceSouthWest.add(mStandingSouthWest);
		mWalkingSequenceSouthWest.add(step21sw);
		mWalkingSequenceSouthWest.add(step22sw);
		mWalkingSequenceSouthWest.add(step21sw);	
		
		mStandingWest = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_w);
		Bitmap step11w = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_w);
		Bitmap step12w = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_w);
		Bitmap step21w = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_w);
		Bitmap step22w = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_w);
	
		mWalkingSequenceWest.add(mStandingWest);
		mWalkingSequenceWest.add(step11w);
		mWalkingSequenceWest.add(step12w);
		mWalkingSequenceWest.add(step11w);
		mWalkingSequenceWest.add(mStandingWest);
		mWalkingSequenceWest.add(step21w);
		mWalkingSequenceWest.add(step22w);
		mWalkingSequenceWest.add(step21w);	
		
		mStandingNorthWest = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_nw);
		Bitmap step11nw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_nw);
		Bitmap step12nw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_nw);
		Bitmap step21nw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_nw);
		Bitmap step22nw = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_nw);
	
		mWalkingSequenceNorthWest.add(mStandingNorthWest);
		mWalkingSequenceNorthWest.add(step11nw);
		mWalkingSequenceNorthWest.add(step12nw);
		mWalkingSequenceNorthWest.add(step11nw);
		mWalkingSequenceNorthWest.add(mStandingNorthWest);
		mWalkingSequenceNorthWest.add(step21nw);
		mWalkingSequenceNorthWest.add(step22nw);
		mWalkingSequenceNorthWest.add(step21nw);	
		
		mStandingNorth = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_n);
		Bitmap step11n = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_n);
		Bitmap step12n = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_n);
		Bitmap step21n = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_n);
		Bitmap step22n = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_n);
	
		mWalkingSequenceNorth.add(mStandingNorth);
		mWalkingSequenceNorth.add(step11n);
		mWalkingSequenceNorth.add(step12n);
		mWalkingSequenceNorth.add(step11n);
		mWalkingSequenceNorth.add(mStandingNorth);
		mWalkingSequenceNorth.add(step21n);
		mWalkingSequenceNorth.add(step22n);
		mWalkingSequenceNorth.add(step21n);	
		
		mStandingNorthEast = BitmapFactory.decodeResource(context.getResources(), R.drawable.standing_ne);
		Bitmap step11ne = BitmapFactory.decodeResource(context.getResources(), R.drawable.step11_ne);
		Bitmap step12ne = BitmapFactory.decodeResource(context.getResources(), R.drawable.step12_ne);
		Bitmap step21ne = BitmapFactory.decodeResource(context.getResources(), R.drawable.step21_ne);
		Bitmap step22ne = BitmapFactory.decodeResource(context.getResources(), R.drawable.step22_ne);
	
		mWalkingSequenceNorthEast.add(mStandingNorthEast);
		mWalkingSequenceNorthEast.add(step11ne);
		mWalkingSequenceNorthEast.add(step12ne);
		mWalkingSequenceNorthEast.add(step11ne);
		mWalkingSequenceNorthEast.add(mStandingNorthEast);
		mWalkingSequenceNorthEast.add(step21ne);
		mWalkingSequenceNorthEast.add(step22ne);
		mWalkingSequenceNorthEast.add(step21ne);	
		
		mSpriteWidth = mStandingEast.getWidth();
		mSpriteHeight = mStandingEast.getHeight();
		mChangeSequenceTime = WALKING_SEQUENCE_LENGTH / mWalkingSequenceEast.size();
		
		Log.d("Knight", "ChangeSequenceTime = " + mChangeSequenceTime);
		
		mNextBitmap = mStandingEast;
	}
	
	@Override
	public void update(float delta){
		//update the character's position
		if(mDestinationY < (int) mPosition.y){
			mDirectionY = NORTH;
			mPosition.y += mDirectionY * VELOCITY * delta; 
		} else if(mDestinationY > (int) mPosition.y){
			mDirectionY = SOUTH;
			mPosition.y += mDirectionY * VELOCITY * delta;			 
		} else{
			mDirectionY = 0;
		}
		if(mDestinationX < (int) mPosition.x){
			mDestinationX = WEST;
			mPosition.x += mDestinationX * VELOCITY * delta;			 
		} else if(mDestinationX > (int) mPosition.x){
			mDestinationX = EAST;
			mPosition.x += mDestinationX * VELOCITY * delta; 
		} else{
			mDirectionX = 0;
		}
		//TODO: weitermachen
		//if the character is not moving, display the standing bitmap
		if(mDestinationY == (int) mPosition.y && mDestinationX == (int) mPosition.x){
			mNextInSequence = 0;
			mLastChangeElapsedTime = 0;
		} else{
			mLastChangeElapsedTime += delta;			
			final int i = (int) (mLastChangeElapsedTime / mChangeSequenceTime);
			if(i > 0){				
				mNextInSequence += i; 
				if(mNextInSequence >= mWalkingSequenceEast.size()){
					mNextInSequence = 0;
				}
				
				if(mDirectionX == EAST){
					if(mDirectionY == NORTH){
						Log.d("Knight", "Walking NorthEast");
						mNextBitmap = mWalkingSequenceNorthEast.get(mNextInSequence);
					} else if(mDirectionY == SOUTH){
						Log.d("Knight", "Walking SouthEast");
						mNextBitmap = mWalkingSequenceSouthEast.get(mNextInSequence);
					} else{
						Log.d("Knight", "Walking East");
						mNextBitmap = mWalkingSequenceEast.get(mNextInSequence);
					}
				} else if(mDirectionX == WEST){
					if(mDirectionY == NORTH){
						Log.d("Knight", "Walking NorthWest");
						mNextBitmap = mWalkingSequenceNorthWest.get(mNextInSequence);
					} else if(mDirectionY == SOUTH){
						Log.d("Knight", "Walking SouthWest");
						mNextBitmap = mWalkingSequenceSouthWest.get(mNextInSequence);
					} else{
						Log.d("Knight", "Walking West");
						mNextBitmap = mWalkingSequenceWest.get(mNextInSequence);
					}
				} else if(mDirectionY == NORTH){
					Log.d("Knight", "Walking North");
					mNextBitmap = mWalkingSequenceNorth.get(mNextInSequence);
				} else if(mDirectionY == SOUTH){
					Log.d("Knight", "Walking South");
					mNextBitmap = mWalkingSequenceSouth.get(mNextInSequence);
				}
				if(mDirectionX == 0 && mDirectionY ==0){
					mNextBitmap = mStandingSouthWest;
				}
				mLastChangeElapsedTime = 0;
			}
		}		
	}

	@Override
	public void render(Canvas canvas) {
		canvas.drawBitmap(mNextBitmap, mPosition.x - (mSpriteWidth / 2), mPosition.y - (mSpriteHeight / 2), null);
	}	
}
